#
# RhythmboxGWT - a GWT interface for Rhythmbox.
# Copyright (C) 2010 Matteo Brusa.
# Copyright (C) 2007 Michael Gratton.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

import cStringIO

import os
import sys
import time
import socket

 
import gtk
import gobject

import rb
import rhythmdb



from wsgiref.simple_server import WSGIRequestHandler
from RhythmboxGWTServer import RhythmboxGWTServer
from DBScanner import DBScanner


class RhythmboxGWTPlugin(rb.Plugin):


    def _load_completed(db, user_data):
        print "pizza _load_completed"


    def __init__(self):
        super(RhythmboxGWTPlugin, self).__init__()
        print "pizza" 

    def activate (self, shell):
        print "pizza" 
        self.db = shell.props.db
        self.shell = shell
        self.player = shell.get_player()
        self.shell_cb_ids = (
            self.player.connect ('playing-song-changed',
                                 self._playing_entry_changed_cb),
            self.player.connect ('playing-changed',
                                 self._playing_changed_cb)
            )
        self.db_cb_ids = (
            self.db.connect ('entry-extra-metadata-notify',
                             self._extra_metadata_changed_cb),
            self.db.connect ('load-complete',
                             self._load_completed),
            self.db.connect ('save-complete',
                             self._load_completed)
            )
             
        self.port = 8000
        self.server = RhythmboxGWTServer('', self.port, self)
        
        self.scanner= DBScanner(self.shell)
 

    def deactivate(self, shell):
#        self.server.shutdown()
#        self.server = None

        for id in self.shell_cb_ids:
            self.player.disconnect(id)

        for id in self.db_cb_ids:
            self.db.disconnect(id)

        self.player = None
        self.shell = None
        self.db = None

     

    def _playing_changed_cb(self, player, playing):
        self._update_entry(player.get_playing_entry())

    def _playing_entry_changed_cb(self, player, entry):
        self._update_entry(entry)

    def _extra_metadata_changed_cb(self, db, entry, field, metadata):
        if entry == self.player.get_playing_entry():
            self._update_entry(entry)

    def _update_entry(self, entry):
        if entry:
            artist = self.db.entry_get(entry, rhythmdb.PROP_ARTIST)
            album = self.db.entry_get(entry, rhythmdb.PROP_ALBUM)
            title = self.db.entry_get(entry, rhythmdb.PROP_TITLE)
            stream = None
            stream_title = \
                self.db.entry_request_extra_metadata(entry,
                                                     'rb:stream-song-title')
            if stream_title:
                stream = title
                title = stream_title
                if not artist:
                    artist = self.db.\
                        entry_request_extra_metadata(entry,
                                                     'rb:stream-song-artist')
                if not album:
                    album = self.db.\
                            entry_request_extra_metadata(entry,
                                                         'rb:stream-song-album')
            self.server.set_playing(artist, album, title, stream)
        else:
            self.server.set_playing(None, None, None, None)









